package com.itheima.test02;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
第二题.使用SimpleDateFormat对象完成 字符串和Date对象的互相转换
 */
public class Test02 {
    public static void main(String[] args) throws ParseException {
        String s = "1997-02-14 00:00:00";
        Date strToDate = strToDate(s);//将字符串转换为Date格式

        Date now = new Date();
        String dateToStr = dateToStr(now);//将Date格式转换为字符串格式

        System.out.println("将字符串转换为Date格式：" + strToDate);
        System.out.println("将Date格式转换为字符串格式：" + dateToStr);
    }

    private static String dateToStr(Date now) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateToStr = simpleDateFormat.format(now);
        return dateToStr;
    }

    private static Date strToDate(String s) throws ParseException {
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将字符串转换为Date格式
        Date strToDate = simpleDateFormat.parse(s);
        return strToDate;
    }


}
